<?php

class ImageController extends Controller
{
	public function actionIndex($pagenum)
	{
		
		if(isset($_GET['pn']))
			$pagenum = $_GET['pn'];
		if(isset($_GET['ps']))
			$pagesize = $_GET['ps'];
	
		if(isset($_GET['type']) && $_GET['type'] == "admin"){
			$criteria = array(
				'condition'=>'status!='.Image::STATUS_DISABLED,
		        'order'=>'datetime DESC',
		    );
		} else {
			$criteria = array(
		        //'condition'=>'status = '.Image::STATUS_PUBLISHED,
		        'condition'=>'status!='.Image::STATUS_DISABLED,
		        'order'=>'datetime DESC',
		        //'with'=>array('author'),
		    );
		}
		
     	$dataProvider = new CActiveDataProvider('Image', array(
            'pagination'=>array(
            	'pageSize'=> 15,
            	'currentPage'=> $pagenum,
            	),
            'criteria'=> $criteria,
        ));

     	$data = $dataProvider->getData();
        
        $result = array();
        foreach ($data as $key => $value) {
         	$result[] = $value->attributes;
        }
        $this->responseJSON($result);
	}

	public function actionCreate()
	{
		$model = new Image;
		$value = $_POST;
		$model->attributes = array(
			'desc' => $value['desc'],
			'height' => $value['imageHeight'],
			'width' => $value['imageWidth'],
			'source' => $value['objUrl'],
			'refer' => $value['fromUrl'],
			'datetime' => time(),
		 );
		
		if($model->save()){
			$this->responseJSON(array());
		};
	}

	public function actionCrawl()
	{
		$url = "http://pic.baidu.com/channel/imgs?c=%E7%BE%8E%E9%A3%9F&t=%E5%85%A8%E9%83%A8&s=0&pn=60&rn=60";
		$output = Yii::app()->curl->get($url,array());
		$result = json_decode($output,true);
		$imgs = $result['imgs'];

		foreach($imgs as $key => $value){
			if($value){
				$model = new Image;
				$model->attributes = array(
					'desc' => $value['desc'],
					'height' => $value['imageHeight'],
					'width' => $value['imageWidth'],
					'source' => $value['objUrl'],
					'refer' => $value['fromUrl'],
					'datetime' => time(),
				 );
				
				if($model->save()){
					
				};
			}		
		}
		$this->responseJSON(array());
	}

	public function actionPublish(){
		if(isset($_POST['id'])){
			$id = $_POST['id'];
		}else{
			$this->responseJSON("no id");
		}
		
		$model = $this->loadModelbyId($id);
		$tmp = $model->attributes;
		$tmp['status'] = Image::STATUS_PUBLISHED;
		$model->attributes = $tmp;
		if($model->save()){
			$this->responseJSON(array());
		}else{
			$this->responseJSON("error");
		}
	}

	public function actionDisable($id){
		if(isset($_POST['id'])){
			$id = $_POST['id'];
		}else{
			$this->responseJSON("no id");
		}
		$model = $this->loadModelbyId($id);
		$tmp = $model->attributes;
		$tmp['status'] = Image::STATUS_DISABLED;
		$model->attributes = $tmp;
		if($model->save()){
			$this->responseJSON("success");
		}
	}

	public function actionSetTag($id){
	}

	public function actionSetDesc(){
		$model = $this->loadModelbyId($id);
		$model->attributes['desc'] = $_POST['desc'];
		if($model->save()){
			$this->responseJSON(array());
		}
	}

	public function actionDelete(){
		if(isset($_POST['id'])){
			$id = $_POST['id'];
		}else{
			$this->responseJSON("no id");
		}
		$model = $this->loadModelbyId($id);
		$tmp = $model->attributes;
		$tmp['status'] = Image::STATUS_DISABLED;
		$model->attributes = $tmp;
		if($model->save()){
			$this->responseJSON(array());
		}
	}

	public function loadModelbyId($id)
	{
		$model=Image::model()->findByPk($id);
		if($model===null)
			$this->responseJSON("wrong id");
		return $model;
	}
}